﻿
namespace Apache.Cassandra.Client {
    using System;

    public class CassandraConnection : IDisposable {

        private object connectionString;
        private CassandraConnectionString options;
        private CassandraInternalConnection connection;

        public CassandraConnection() {
        }
        public CassandraConnection(object connectionString) {
            ConnectionString = connectionString;
        }

        public object ConnectionString {
            get {
                return connectionString;
            }
            set {
                options = new CassandraConnectionString(value);
                connectionString = value;
            }
        }

        [CLSCompliant(false)]
        public Cassandra.Iface Client {
            get {
                return connection.Client;
            }
        }

        internal CassandraConnectionString Options {
            get {
                return options;
            }
        }

        public void Open() {
            if (null != connection) {
                throw new InvalidOperationException();
            }
            if (null == options) {
                throw new InvalidOperationException();
            }

            connection = CassandraConnectionFactory.Factory.GetConnection(this);
        }

        public void Close() {
            if (null == connection) {
                throw new InvalidOperationException();
            }

            connection.Close(this);
            connection = null;
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private bool disposed = false;
        private void Dispose(bool disposing) {
            if (disposed) {
                return;
            }

            if (disposing) {
                if (null != connection) {
                    Close();
                }
            }

            disposed = true;
        }
    }
}
